// Copyright 2011, Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package v201101;

import com.google.api.adwords.lib.AdWordsServiceLogger;
import com.google.api.adwords.lib.AdWordsUser;
import com.google.api.adwords.lib.AuthToken;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * This example gets and downloads a report from a report definition.
 * To get a report definition, run AddKeywordsPerformanceReportDefinition.java.
 * Currently, there is only production support for report download.
 *
 * @author api.arogal@gmail.com (Adam Rogal)
 */
public class DownloadReport {
  public static void main(String[] args) {
    try {
      // Log SOAP XML request and response.
      AdWordsServiceLogger.log();

      // Get AdWordsUser from "~/adwords.properties".
      AdWordsUser user = new AdWordsUser();
      user.setAuthToken(new AuthToken(user.getEmail(), user.getPassword()).getAuthToken());

      String fileName = "INSERT_OUTPUT_FILE_NAME_HERE";
      long reportDefintionId = Long.parseLong("INSERT_REPORT_DEFINITION_ID_HERE");

      String url = "https://adwords.google.com/api/adwords/reportdownload?__rd="
          + reportDefintionId;

      HttpURLConnection urlConn = (HttpURLConnection) new URL(url).openConnection();
      urlConn.setRequestMethod ("GET");
      urlConn.setRequestProperty("Authorization", "GoogleLogin auth="
          + user.getRegisteredAuthToken());

      if (user.getClientCustomerId() != null) {
        urlConn.setRequestProperty("clientCustomerId", user.getClientCustomerId());
      } else if (user.getClientEmail() != null) {
        urlConn.setRequestProperty("clientEmail", user.getClientEmail());
      } else {
        urlConn.setRequestProperty("clientEmail", user.getEmail());
      }
      urlConn.setRequestProperty(
          "returnMoneyInMicros", Boolean.toString(user.isReportsReturnMoneyInMicros()));

      urlConn.connect();
      writeStreamToStream(urlConn.getInputStream(), new FileOutputStream(new File(fileName)));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Writes the contents of the {@link InputStream} inputStream to the
   * {@link OutputStream} outputStream.
   *
   * @param inputStream the {@code InputStream} to read from
   * @param outputStream the {@code OutputStream} to write to
   * @throws IOException if an I/O error occurs
   */
  private static void writeStreamToStream(InputStream inputStream, OutputStream outputStream)
      throws IOException {
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    BufferedOutputStream bos = new BufferedOutputStream(outputStream);

    try {
      int i = 0;
      while ((i = bis.read()) != -1) {
        bos.write(i);
      }
    } finally {
      if (bis != null) {
        bis.close();
      }

      if (bos != null) {
        bos.close();
      }
    }
  }
}
